<?php

// @author 3ogx <3ogx.com@gmail.com>

namespace Jushuitan;

trait ErpHelpTrait
{
    /**
     * 生成 sign.
     *
     * @return null|string
     */
    public function getSign($secret, array $data)
    {
        if (null == $data) {
            return null;
        }
        ksort($data);
        $result_str = '';
        foreach ($data as $key => $val) {
            if (null != $key && '' != $key && 'sign' != $key) {
                $result_str = $result_str . $key . $val;
            }
        }
        $result_str = $secret . $result_str;

        return bin2hex(md5($result_str, true));
    }

    /**
     * @param $data
     *
     * @return null
     */
    public function object_2_array($data)
    {
        $arr = is_object($data) ? get_object_vars($data) : $data;
        $tmp = null;
        foreach ($arr as $key => $val) {
            $value = is_array($val) || is_object($val) ? $this->object_2_array($val) : $val;
            $tmp[$key] = $value;
        }
        return $tmp;
    }
}
